/**
 * 
 */
package com.chitranshu.activities;

import java.util.ArrayList;
import java.util.Stack;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

/**
 * @author anshu
 * 
 */
public abstract class BaseTabActivity extends ActivityGroup implements
		OnClickListener {

	public static final String EXTRA_START_AS_CHILD = "EXTRA_START_AS_CHILD";

	protected ArrayList<Stack<View>> stacks;
	protected ArrayList<Intent> tabContent;

	protected Stack<View> currentStack;
	protected View previousView;

	protected LinearLayout tabLayout;
	protected static final int id = 0x0101;
	protected FrameLayout content;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		tabLayout = new LinearLayout(this);
		tabLayout.setId(id);
		tabLayout.setOrientation(LinearLayout.HORIZONTAL);

		content = new FrameLayout(this);

		android.widget.LinearLayout.LayoutParams tabLp = new android.widget.LinearLayout.LayoutParams(
				android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
				android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
		tabLp.weight = 1.0f;

		int count = getTabCount();
		stacks = new ArrayList<Stack<View>>(count);
		tabContent = new ArrayList<Intent>(count);
		for (int i = 0; i < count; i++) {
			View view = getTabView(i);
			view.setLayoutParams(tabLp);
			view.setPressed(false);
			view.setOnClickListener(this);
			view.setId(i);
			tabLayout.addView(view);
			stacks.add(new Stack<View>());
			tabContent.add(getTabContent(i));

		}
	}

	abstract protected Intent getTabContent(int position);

	abstract protected int getTabCount();

	abstract protected View getTabView(int position);

	protected void setCurrentTab(int position) {
		View view = tabLayout.getChildAt(position);
		view.setPressed(true);
		if (null != previousView) {
			previousView.setPressed(false);
		}
		previousView = view;
		currentStack = stacks.get(position);
		content.removeAllViews();
		if (currentStack.size() == 0) {
			View decorView = getLocalActivityManager().startActivity("ID",
					tabContent.get(position)).getDecorView();
			content.addView(decorView);
			currentStack.add(decorView);
		} else {
			content.addView(currentStack.peek());
		}
	}

	@Override
	public void onClick(View v) {
		if (previousView != v) {
			setCurrentTab(v.getId());
		}
	}

	@Override
	public void startActivityFromChild(Activity child, Intent intent,
			int requestCode) {
		boolean flag = intent.getBooleanExtra(EXTRA_START_AS_CHILD, false);
		if (flag) {
			View decorView = getLocalActivityManager()
					.startActivity("", intent).getDecorView();
			content.addView(decorView);
			currentStack.add(decorView);
		} else {
			super.startActivityFromChild(child, intent, requestCode);
		}
	}

	@Override
	public void finishFromChild(Activity child) {
		if (0 == currentStack.size()) {
			super.finishFromChild(child);
		} else {
			showPreviousView();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && 1 < currentStack.size()) {
			showPreviousView();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	private void showPreviousView() {
		content.removeAllViews();
		currentStack.pop();
		content.addView(currentStack.peek());
	}
}
